package com.calvin.study.multithread.threadpool;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

//线程池拒绝策略
public class ThreadPoolDemo02 {

	public static void main(String[] args) {
		// definedThreadPool(new ThreadPoolExecutor.AbortPolicy());
		definedThreadPool(new ThreadPoolExecutor.CallerRunsPolicy());
	}

	public static void definedThreadPool(RejectedExecutionHandler handler) {
		// 线程最大8个
		ExecutorService executor = new ThreadPoolExecutor(2, 5, 1L, TimeUnit.SECONDS,
				new LinkedBlockingDeque<Runnable>(3), Executors.defaultThreadFactory(), handler);

		try {
			for (int i = 1, len = 10; i <= len; i++) {
				executor.execute(() -> {
					System.out.println("[窗口" + Thread.currentThread().getName() + "] \t 办理业务");
				});
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			executor.shutdown();
		}
	}
}
